package com.ciwong.epaper.ui;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.util.LogOutUtil;
import com.ciwong.libs.utils.CWResource;
import com.ciwong.libs.utils.DeviceUtils;
import com.ciwong.mobilelib.utils.BaseConstants;
import com.ciwong.mobilelib.utils.ThreadTask;

/**
 * 弹出框Activity
 *
 * @author sugy
 * @version ciwong v.1.0 2016/3/30 9:30
 * @since ciwong v.1.0
 */
public class DialogActivity extends Activity
{

    private final int rgb = 93;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.libs_cw_dialog);
        setFinishOnTouchOutside(false);
        setMessage();
        setPositiveButton();
    }

    /**
     * 设置主体信息
     */
    public void setMessage()
    {
        final int dipValue = 8;
        TextView mTextView = new TextView(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.FILL_PARENT);
        lp.gravity = Gravity.CENTER;
        mTextView.setGravity(Gravity.CENTER);
        mTextView.setTextSize(getResources().getInteger(
                CWResource.getIntegerId("libs_cw_dialog_content_font_size")));
        mTextView.setTextColor(getResources()
                .getColor(CWResource.getColorId("libs_dialog_content_color")));
        mTextView.setPadding(DeviceUtils.dip2px(dipValue), 0,
                DeviceUtils.dip2px(dipValue), DeviceUtils.dip2px(dipValue));
        mTextView.setText("\n" + getString(R.string.token_expire) + "\n");
        ViewGroup container = (ViewGroup) findViewById(
                CWResource.getId("cw_dialog_container"));
        container.removeAllViews();
        container.addView(mTextView, lp);
    }

    /**
     * 确认按钮
     *
     */
    public void setPositiveButton()
    {
        Button positive = (Button) findViewById(
                CWResource.getId("cw_dialog_button_ok"));
        final int paddingt = 10;
        final int paddingl = 20;
        positive.setBackgroundResource(R.drawable.dialog_floor_selector);
        positive.setPadding(paddingt, paddingl, paddingt, paddingl);
        positive.setText(android.R.string.ok);
        positive.setTextColor(Color.rgb(rgb, rgb, rgb));
        positive.setTextSize(BaseConstants.DIALOG_MESSAGE_TEXTSIZE);
        positive.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();
                ThreadTask.getInstance().executorOtherThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        LogOutUtil.logOut(DialogActivity.this);
                    }
                }, ThreadTask.ThreadPeriod.PERIOD_HIGHT);
            }
        });
        positive.setVisibility(View.VISIBLE);
        ViewGroup buttonContainer = (ViewGroup) findViewById(
                CWResource.getId("cw_dialog_button_container"));
        buttonContainer.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK)
        {
            return true;
        }
        return super.onKeyDown(keyCode, event);

    }
}
